import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:rc_app/bean/alarm_entity.dart';
import 'package:rc_app/http/alarm_api.dart';
import 'package:rc_app/pages/container_page.dart';
import 'package:rc_app/utils/screen_utils.dart';
import 'package:rc_app/widgets/loading_widget.dart';

AlarmApi alarmApi = AlarmApi();

//日常警情处置二级页面
class AlarmHandle extends StatefulWidget {
  //默认查询类型
  int pageType;

  AlarmHandle({Key key, this.pageType = 0}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _AlarmHandleState();
  }
}

class _AlarmHandleState extends State<AlarmHandle> {
  String title = '';
  String tip = '';
  int _alarmCount = 0;
  List<AlarmListBean> alarmList;

  //初始化title
  void initAppBarTitle() {
    switch (widget.pageType) {
      case 0:
        title = '警情-待接收';
        tip = "共有$_alarmCount条待接收警情";
        break;
      case 1:
        title = '警情-待反馈';
        tip = "共有$_alarmCount条待反馈警情";
        break;
      case 2:
        title = '警情-已反馈';
        tip = "共有$_alarmCount条已反馈警情";
        break;
    }
  }

  @override
  void initState() {
    super.initState();
    initAppBarTitle();
    requestApi();
  }

  requestApi() {
    alarmApi.getAlarmList(widget.pageType, (json) {
      AlarmEntity alarm = AlarmEntity.fromJson(json);
      setState(() {
        alarmList = alarm.data;
        _alarmCount = alarmList.length;
        initAppBarTitle();
      });
    });
  }

  //按钮点击事件
  void _itemClick(int type, AlarmListBean item) {
    print('itemClick ' + item.bjsj);
    Fluttertoast.showToast(msg: item.bjsj);
  }

  Widget getButtonWidget(
      String leftTitle, String rightTitle, int type, AlarmListBean item) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        Expanded(
          flex: 1,
          child: Container(
              color: Colors.lightBlue,
              height: 45.0,
              child: FlatButton(
                onPressed: () {
                  _itemClick(type * 10, item);
                },
                child: Text(
                  leftTitle,
                  style: TextStyle(fontSize: 15, color: Colors.white),
                ),
              )),
        ),
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 1),
        ),
        Expanded(
          flex: 1,
          child: Container(
              color: Colors.lightBlue,
              height: 45.0,
              child: FlatButton(
                onPressed: () {
                  _itemClick(type * 10 + 1, item);
                },
                child: Text(
                  rightTitle,
                  style: TextStyle(fontSize: 15, color: Colors.white),
                ),
              )),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    print('build alarm handle');
    return buildBody();
  }
  Widget getAlarmIcon(AlarmListBean item){
    Color color;
    String jqlx;
    switch(item.jqlx){
      case '1':
        color = Colors.green;
        jqlx = '治安';
        break;
      case '2':
        color = Colors.red;
        jqlx = '交通';
        break;
      default:
        color = Colors.lightBlue;
        jqlx = '一般';
    }
    return Container(
      padding: EdgeInsets.symmetric(vertical: 8,horizontal: 15),
      width: 35,
      height:18,
      color: color,
      child: Text(jqlx,style: TextStyle(color: Colors.white,fontSize: 3),),
    );
  }
  Widget getScrollListItem(int index) {
    var item = alarmList[index];
    return Container(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 20, vertical: 5),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Container(
                  alignment: Alignment.centerRight,
                  child: Text(
                    '报警时间：',
                    style: TextStyle(fontSize: 15),
                  ),
                ),
                Expanded(
                  child: Text(
                    item.bjsj,
                    style: TextStyle(fontSize: 15),
                  ),
                ),

              ],
            ),
          ),
          Divider(
            indent: 20,
            endIndent: 20,
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 20, vertical: 5),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  '发生地点：',
                  style: TextStyle(fontSize: 15, color: Colors.black54),
                ),
                Expanded(
                  child: Text(
                    item.fsdd,
                    style: TextStyle(fontSize: 15),
                  ),
                ),
              ],
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 20, vertical: 5),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Container(
                  width: 80,
                  alignment: Alignment.centerRight,
                  child: Text(
                    '报警描述：',
                    style: TextStyle(fontSize: 15, color: Colors.black54),
                  ),
                ),
                Expanded(
                  child: Text(
                    item.jqms,
                    style: TextStyle(fontSize: 15, color: Colors.black54),
                  ),
                ),
              ],
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 20, vertical: 5),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Container(
                  width: 80,
                  alignment: Alignment.centerRight,
                  child: Text(
                    '报警人：',
                    style: TextStyle(fontSize: 15, color: Colors.black54),
                  ),
                ),
                Expanded(
                  child: Text(
                    item.bjr,
                    style: TextStyle(fontSize: 15),
                  ),
                ),
              ],
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 20, vertical: 5),
            child: Row(
              children: <Widget>[
                Text(
                  '报警电话：',
                  style: TextStyle(fontSize: 15, color: Colors.black54),
                ),
                Expanded(
                  child: Text(
                    item.bjdh,
                    style: TextStyle(fontSize: 15),
                  ),
                ),
                RawMaterialButton(
                  child: Text('查看详情',style:TextStyle(color:Colors.lightBlue)),
                  onPressed: () {
                    _itemClick(3, item);
                  },
                ),
              ],
            ),
          ),
          Stack(
            children: <Widget>[
              Offstage(
                offstage: !(widget.pageType == 0),
                child: getButtonWidget("接收", "拒绝", 1, item),
              ),
              Offstage(
                offstage: !(widget.pageType == 1),
                child: getButtonWidget("处置反馈", "返回", 1, item),
              )
            ],
          )
        ],
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          top: BorderSide(width: 1, color: Color(0xd5d7dc)),
        ),
      ),
      margin: EdgeInsets.symmetric(vertical: 8),
    );
  }

  Future<Null> _onRefresh() async {
    requestApi();
  }

  Widget getScrollBody() {
    if (alarmList == null) {
      return Center(
          child: LoadingWidget.getLoading(backgroundColor: Colors.transparent));
    }
    return Column(
      children: <Widget>[
        Container(
          height: 35,
          padding: EdgeInsets.only(right: 15),
          alignment: Alignment.centerRight,
          child: Text(
            tip,
            style: TextStyle(
                color: Colors.black45,
                letterSpacing: 1,
                fontWeight: FontWeight.bold),
          ),
        ),
        Expanded(
            child: RefreshIndicator(
          child: CustomScrollView(
            physics: const BouncingScrollPhysics(),
            slivers: <Widget>[
              SliverList(
                delegate: SliverChildBuilderDelegate(
                    (BuildContext context, int index) {
                  return getScrollListItem(index);
                }, childCount: alarmList.length),
              )
            ],
          ),
          onRefresh: _onRefresh,
        ))
      ],
    );
  }

  Widget buildBody() {
    return Scaffold(
        appBar: AppBar(
          title: Text(
            title,
            style: TextStyle(color: Colors.white),
          ),
        ),
        body: Container(
            height: ScreenUtils.screenH(context),
            width: ScreenUtils.screenW(context),
            color: Color(0xf3f5f7),
            child: getScrollBody()));
  }
}
